package org.rescue.data.extraction;


import java.util.*;

import org.rescue.data.extraction.*;
import org.rescue.search.Document;
import org.apache.commons.collections.map.*;


/**
 * Simple classification for testing other components.  
 * The ids assigned here need to match the database 
 * KML entries in order for the results to make sense.  
 * 
 * @author jlickfet
 *
 */
public class ClassifySimpleLocation implements ClassifyBehavior {
	
	public static final int TAX_US_GAZETEER = 100; 
	
	private MultiValueMap locLookup;
	
	public ClassifySimpleLocation() {

		locLookup = new MultiValueMap(); // key=string, value=ints
		
		locLookup.put("US", 1);
		locLookup.put("United States", 1);
		locLookup.put("Louisiana", 2);
		locLookup.put("LA", 2);
		locLookup.put("Alabama", 3);
		locLookup.put("AL", 3);
		locLookup.put("California", 4);
		locLookup.put("CA", 4);
		locLookup.put("Florida", 5);
		locLookup.put("FL", 5);
		locLookup.put("Mississippi", 6);
		locLookup.put("MS", 6);
		locLookup.put("Texas", 7);
		locLookup.put("TX", 7);
		
		locLookup.put("Florida", 8);  // pretend city of 'Florida'
		locLookup.put("California", 9);  //another pretend city
		
	}
	
	/**
	 * Sets the location ids for all the documents in the dataset.
	 */
	public void classify(ArrayList<Document> documents) {
		for (Document d : documents) {
			for (Location l : d.getLocations()) {
		
				// for now assign equal probabilities if there are multiple matches
				ArrayList<Integer> matches = lookupId(l.getName()); 	
				double prob = 1.0/matches.size();
				
				// create a new concept set for each set of OR'd possible locations
				// add each concept to the concept set
				// assign the concept set to the document
				AnnotationSet as = new AnnotationSet(TAX_US_GAZETEER);
				for (Integer match : matches) {
					Annotation a = new Annotation(l.getName(), match, prob);					
					as.addAnnotation(a);					
				}
				d.addAnnotationSet(as);
			}
		}
	}
	
	/**
	 * @param name the location to find an id for
	 * @return returns <code>ArrayList<Integer></code>  of ids of locations or -1 if not found 
	 */
	private ArrayList<Integer> lookupId(String name){	
		if (locLookup.getCollection(name) != null) {
			return (ArrayList<Integer>)locLookup.getCollection(name);
		}
		else { 
			ArrayList<Integer> notfound = new ArrayList<Integer>();
			notfound.add(-1);
			return notfound;
		}
	}
	
}
